<?php
/**
 * LowCalVanilla Functions tests
 *
 * @package Extensions
 * @subpackage test
 * @copyright Damien Lebrun 2007
 * @author Damien Lebrun
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GPLv2
 */

if (!defined('IN_VANILLA')) {
	require('./ini_test.php');

	define('STANDALONE_TEST', 1);
}

/**
 * LowCalVanilla Functions tests
 * @package Extensions
 */
class TestUtilities extends UnitTestCase {

	var $Context;
	var $PeopleScripts = array('js/global.js');
	var $VanillaScripts = array('js/global.js', 'js/vanilla.js', 'js/ajax.js', 'js/ac.js');

	function setUp() {
		$this->Context = new TestContext();
	}


	function SetHead($Page, $Scripts = array()) {
		$Head = new Head($this->Context);
		if ($Page == 'vanilla') {
			$Scripts = array_merge($this->VanillaScripts, $Scripts);
		} else if ($Page == 'people') {
			$Scripts = array_merge($this->PeopleScripts, $Scripts);
		}
		foreach ($Scripts as $script) {
			$Head->AddScript($script);
		}
		return $Head;
	}

	function testAddScript() {
		$Head = $this->SetHead('vanilla');
		$this->assertTrue(LowCalVanilla_AddScript($this->Context, $Head, 'js/foo.js'));
		$this->assertTrue(is_array($this->Context->PassThruVars['LOWCALVANILLA_SCRIPTS']));
		$this->assertEqual(
			count($this->VanillaScripts),
			$this->Context->PassThruVars['LOWCALVANILLA_SCRIPTS_CLEANUP_POSITION']);
		$this->assertEqual('js/foo.js', $this->Context->PassThruVars['LOWCALVANILLA_SCRIPTS'][300]);

		$Head = $this->SetHead('vanilla');
		unset($this->Context->PassThruVars['LOWCALVANILLA_SCRIPTS']);
		unset($this->Context->PassThruVars['LOWCALVANILLA_SCRIPTS_CLEANUP_POSITION']);
		$this->assertTrue(LowCalVanilla_AddScript($this->Context, $Head, 'js/foo.js'));
		$this->assertTrue(LowCalVanilla_AddScript($this->Context, $Head, 'js/foo.js'));
		$this->assertTrue(LowCalVanilla_AddScript($this->Context, $Head, 'js/bar.js'));
		$this->assertTrue(LowCalVanilla_AddScript($this->Context, $Head, 'js/baz.js', 100));
		$this->assertTrue(is_array($this->Context->PassThruVars['LOWCALVANILLA_SCRIPTS']));
		$this->assertEqual(
			count($this->VanillaScripts),
			$this->Context->PassThruVars['LOWCALVANILLA_SCRIPTS_CLEANUP_POSITION']);
		$this->assertEqual('js/foo.js', $this->Context->PassThruVars['LOWCALVANILLA_SCRIPTS'][300]);
		$this->assertEqual('js/bar.js', $this->Context->PassThruVars['LOWCALVANILLA_SCRIPTS'][301]);
		$this->assertEqual('js/baz.js', $this->Context->PassThruVars['LOWCALVANILLA_SCRIPTS'][100]);

		$Head = $this->SetHead('vanilla');
		unset($this->Context->PassThruVars['LOWCALVANILLA_SCRIPTS']);
		unset($this->Context->PassThruVars['LOWCALVANILLA_SCRIPTS_CLEANUP_POSITION']);
		$this->Context->Configuration['LOWCALVANILLA_PACKER_ENABLE'] = '0';
		$this->assertFalse(LowCalVanilla_AddScript($this->Context, $Head, 'js/foo.js'));
		$this->assertFalse(is_array($this->Context->PassThruVars['LOWCALVANILLA_SCRIPTS']));
		$this->assertEqual('/js/foo.js', $Head->Scripts[4]);
	}

	function testCleanUp() {
		$Head = $this->SetHead('vanilla', array('js/bar.js'));
		$GLOBALS['Head'] = &$Head;
		$this->Context->PassThruVars['LOWCALVANILLA_SCRIPTS'][300] = 'js/foo.js';
		$this->Context->PassThruVars['LOWCALVANILLA_SCRIPTS_CLEANUP_POSITION'] = 4;
		$CleanUp = new LowCalVanilla_CleanUp($this->Context);
		$CleanUp->Render();
		$this->assertNull($this->Context->PassThruVars['LOWCALVANILLA_SCRIPTS']);
		$this->assertEqual('/js/foo.js', $Head->Scripts[4]);
		$this->assertEqual('/js/bar.js', $Head->Scripts[5]);
	}


}

if (defined('STANDALONE_TEST')) {

	$test = new TestUtilities();
	$test->run(new HtmlReporter());
}